<template>
  <view>
    <!-- 搜索 -->
    <view class="search-box">
      <my-search></my-search>
    </view>
    
    <!-- 轮播图 -->
    <swiper :circular="true" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item,i) in swiperList" :key="i" >
        <navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    
    <!-- 菜单导航 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item, i) in navList" :key="i" @click="navClickHandler(item)">
        <image :src="item.image_src"></image>
      </view>
    </view>
    
    <!-- 楼层数据 -->
    <view class="floor-list">
      
      <view class="floor-item" v-for="(item, i) in floorList" :key="i">
        
        <!-- 楼层title -->
        <view class="floor-title">
          <image :src="item.floor_title.image_src"></image>
        </view>
        
        <!-- 楼层图片 -->
        <view class="floor-img">
          <!-- 左边图片 -->
          <navigator class="floor-img-left" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
          </navigator>
          <!-- 右边图片 -->
          <view class="floor-img-right">
            <navigator class="floor-img-right-item" :url="item2.url" v-for="(item2, i2) in item.product_list" v-if="i2 !== 0" :key="i2">
              <image :src="item2.image_src" :style="{width: item2.image_width+'rpx'}" mode="widthFix"></image>
            </navigator>
          </view>
        </view>
        
      </view>
      
    </view>
    
    
  </view>
</template>

<script>
  import setTabBar from '@/mixin/setTabBar.js'
  
  export default {
    mixins: [setTabBar], 
    data() {
      return {
        swiperList: [], // 轮播图数据
        navList: [], // 导航数据
        floorList: [], // 楼层数据
      };
    },
    onLoad() {
      // 发起请求
      this.getSwiperList() // 轮播图
      this.getNavList() // 导航
      this.getFloorList() // 楼层数据
    },
    methods: {
      // 获取首页轮播图数据
      async getSwiperList(){
        // 发起请求
         const { data : res} = await uni.$http.get('/home/swiperdata')
         console.log(res)
         
         // 异常判断
         if(res.meta.status !== 200) return uni.$showMsg()
         
         // 设置页面数据
         this.swiperList = res.message
         
      },
      // 获取菜单导航数据
      async getNavList() {
        // 发起请求
        const { data: res} = await uni.$http.get('/home/catitems')
        console.log('==>', res)
        
        // 异常判断
        if(res.meta.status !== 200) return uni.$showMsg()
        
        // 设置页面数据
        this.navList = res.message
        
      },
      // 菜单导航事件处理函数
      navClickHandler(item){
        console.log(item)
        if(item.name === '分类'){
          // 跳转到分类tabbar
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      // 获取楼层数据
      async getFloorList() {
        const { data: res } = await uni.$http.get('/home/floordata')
        console.log(res)
        // 异常判断
        if(res.meta.status !== 200) return uni.$showMsg()
        
        // 修改数据格式
        res.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            prod.url = `/subpkg/goods_list/goods_list?${prod.navigator_url.split('?')[1]}`
          })
        })
        
        // 设置 页面数据
        this.floorList = res.message
      }
    },
    
    
  }
</script>

<style lang="scss">
  
  .search-box {
    position: sticky;
    top: 0;
    // left: 0;
    z-index: 9;
  }
  
  swiper {
    height: 330rpx;
    
    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }
  
  .nav-list {
    display: flex;
    justify-content: space-around;
    padding: 32rpx 0;
    .nav-item {
      
      // margin: 32rpx 0;
      
      image {
        width: 128rpx;
        height: 140rpx;
      }
    }
  }
  
  .floor-list {
    
    .floor-item {
      
      .floor-title image {
        height: 60rpx;
        width: 100%;
      }
      
      .floor-img {
        display: flex;
        padding-left: 8rpx;
        .floor-img-left {
          
        }
        .floor-img-right {
          display: flex;
          flex-wrap: wrap;
          justify-content: space-around;
        }
      }
    }
  }
  
</style>
